package com.ultimatech.eurekaclient.service.impl;

import com.ultimatech.cloud.poc.base.model.Message;
import com.ultimatech.cloud.poc.base.model.UserVo;
import com.ultimatech.eurekaclient.dao.IUserDao;
import com.ultimatech.eurekaclient.model.User;
import com.ultimatech.eurekaclient.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by zhangleping on 2017/10/31.
 */
@Service
@Transactional
public class UserServiceImpl implements IUserService {

    @Autowired
    private IUserDao userDao;

    @Override
    @Transactional(readOnly = true)
    public Message<UserVo> getById(int id) {
        Message<UserVo> message=new Message<UserVo>();
        User user=userDao.getById(id);
        UserVo vo=new UserVo();
        vo.setName(user.getName());
        vo.setNumber(user.getNumber());
        vo.setId(user.getId());
        vo.setCreateTime(user.getCreateTime());
        message.setData(vo);
        message.setStatus("success");
        return message;
    }

    @Override
    @Transactional(readOnly = true)
    public Message<UserVo> getByNumber(String number) {
        Message<UserVo> message=new Message<UserVo>();
        User user=userDao.getByNumber(number);
        UserVo vo=new UserVo();
        vo.setName(user.getName());
        vo.setNumber(user.getNumber());
        vo.setId(user.getId());
        vo.setCreateTime(user.getCreateTime());
        message.setStatus("success");
        message.setData(vo);
        return message;
    }

    @Override
    public Message<UserVo> addUser(User user,boolean throwEx) {
        user.setCreateTime(new Date());
        int id= this.userDao.addUser(user);
        UserVo vo=new UserVo();
        vo.setName(user.getName());
        vo.setNumber(user.getNumber());
        vo.setId(user.getId());
        vo.setCreateTime(user.getCreateTime());
        Message<UserVo> message=new Message<UserVo>();
        message.setData(vo);
        message.setStatus("success");
        return message;
    }

    @Override
    public Message<Integer> deleteUserById(int id) {
        this.userDao.deleteUserById(id);
        Message<Integer> message=new Message<Integer>();
        message.setData(id);
        message.setStatus("success");
        return message;
    }

    @Override
    public Message<UserVo> updateUser(User user) {
        Message<UserVo> message=new Message<UserVo>();
        user=this.userDao.updateUser(user);
        UserVo vo=new UserVo();
        vo.setName(user.getName());
        vo.setNumber(user.getNumber());
        vo.setId(user.getId());
        vo.setCreateTime(user.getCreateTime());
        message.setStatus("success");
        message.setData(vo);
        return message;
    }

    @Override
    public Message<UserVo> updateUser(UserVo vo) {
        User user=this.userDao.getById(vo.getId());
        user.setName(vo.getName());
        user.setNumber(vo.getNumber());
        user=this.userDao.updateUser(user);
        vo=new UserVo();
        vo.setName(user.getName());
        vo.setNumber(user.getNumber());
        vo.setId(user.getId());
        vo.setCreateTime(user.getCreateTime());
        Message<UserVo> message=new Message<UserVo>();
        message.setData(vo);
        message.setStatus("success");
        return message;
    }

    @Override
    public Message<List<UserVo>> findAll() {
        Message<List<UserVo>> message=new Message<List<UserVo>>();
        List<User> list=this.userDao.findAll();
        message.setData(new ArrayList<UserVo>());
        for(User user:list){
            UserVo vo=new UserVo();
            vo.setName(user.getName());
            vo.setNumber(user.getNumber());
            vo.setId(user.getId());
            vo.setCreateTime(user.getCreateTime());
            message.getData().add(vo);
        }
        message.setStatus("success");
        return message;
    }


}
